/*
 * This file is part of Shopping Lists Organizer software
 *
 * Copyright (C) 2011 Denis Kvita <dkvita@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <QtGui>
#include <QtSql>

#include "shopdelegate.h"

shopDelegate::shopDelegate(QObject *parent) :
	QStyledItemDelegate(parent)
{
}

void shopDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	QStyledItemDelegate::paint(painter, option, index);
}

QSize shopDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	return QStyledItemDelegate::sizeHint(option, index);
}

QWidget* shopDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	if(index.column() > 3){
		if(index.column() == 5 || index.column() == 8){
			QDoubleSpinBox *ded= new QDoubleSpinBox(parent);
			ded->setDecimals(3);
			ded->setMaximum(1000000000.00);
			return ded;
		}
		return QStyledItemDelegate::createEditor(parent, option, index);
	}
	QComboBox *ed= new QComboBox(parent);
	ed->setEditable(true);
	ed->setInsertPolicy(QComboBox::InsertAfterCurrent);
	ed->completer()->setCompletionMode(QCompleter::PopupCompletion);
	ed->setDuplicatesEnabled(false);

	QSqlDatabase dbc= QSqlDatabase::database("CN");
	QSqlQuery q(dbc);
	switch(index.column()){
	case 0: q.prepare("SELECT DISTINCT sTitle,sAddress FROM shop ORDER BY sTitle");
	break;
	case 1: q.prepare("SELECT DISTINCT pSec FROM purchase ORDER BY pSec");
	break;
	case 2: q.prepare("SELECT DISTINCT pCat FROM purchase ORDER BY pCat");
	break;
	default: q.prepare("SELECT DISTINCT pTitle FROM purchase ORDER BY pTitle");
	}
	if(!q.exec()){
		qCritical()<<"Can't get shops list!";
	}
	while(q.next()){
		ed->addItem(q.value(0).toString());
		if(q.record().count() ==2) ed->setItemData(ed->count()-1, q.value(1).toString().trimmed(), Qt::ToolTipRole);
	}

	QString v= qVariantValue<QString>(index.data());
	ed->setCurrentIndex(ed->findText(v));
	return ed;
}

void shopDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
	if(index.column() > 3) return QStyledItemDelegate::setEditorData(editor, index);
	if(qVariantCanConvert<QString>(index.data())){
		QString shopTitle= qVariantValue<QString>(index.data());
		QComboBox *shopEditor= qobject_cast<QComboBox *>(editor);
		shopEditor->setCurrentIndex(shopEditor->findText(shopTitle));
	}else{
		QStyledItemDelegate::setEditorData(editor, index);
	}
}

void shopDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	if(index.column() > 3) return QStyledItemDelegate::setModelData(editor, model, index);
	if (qVariantCanConvert<QString>(index.data())){
		QComboBox *shopEditor = qobject_cast<QComboBox *>(editor);
		model->setData(index, qVariantFromValue(shopEditor->currentText().trimmed()));
	}else{
		QStyledItemDelegate::setModelData(editor, model, index);
	}
}

void shopDelegate::commitAndCloseEditor()
{
	QComboBox *editor= qobject_cast<QComboBox *>(sender());
	emit commitData(editor);
	emit closeEditor(editor);
}

